
var kico = {};

// 弹框
kico.notice_list = document.createElement("div");
kico.notice_list.classList.add("bk-notice-list");

function bk_notice(content, attr) {
    var notice_item = document.createElement("div");
    notice_item.className = "bk-notice";
    notice_item.innerHTML += "<span class='content'>" + content + "</span>";

    kico.notice_list.appendChild(notice_item);

    if(!document.querySelector("body > .bk-notice-list")){document.body.appendChild(kico.notice_list);}

    if(attr && attr.time){
        setTimeout(notice_remove, attr.time);
    }
    else{
        var close = document.createElement("span");
        close.className = "close";

        close.addEventListener("click", function () {
            notice_remove();
        });

        notice_item.classList.add("dismiss");
        notice_item.appendChild(close);
    }

    if(attr && attr.color){notice_item.classList.add(attr.color);}
    if(attr && attr.time && attr.overlay === true){bk_overlay({time: attr.time});}

    function notice_remove() {
        notice_item.classList.add("remove");

        setTimeout(function () {
            try{
                kico.notice_list.removeChild(notice_item);
                document.querySelector("body > .bk-notice-list").removeChild(notice_item);
            }
            catch(err) {}

            if(document.querySelector("body > .bk-notice-list") && kico.notice_list.childNodes.length === 0){
                document.body.removeChild(kico.notice_list);
            }
        }, 300);
    }
}

// 遮罩
kico.overlay = document.createElement("div");
kico.overlay.classList.add("bk-overlay");

function bk_overlay(attr){
    document.body.appendChild(kico.overlay);

    if(attr && attr.time){
        setTimeout(overlay_remove, attr.time);
    }
    else{
        kico.overlay.addEventListener("click", function () {
            overlay_remove();
        });
    }

    if(attr && attr.code){
        kico.overlay.addEventListener("click", function () {
            attr.code();
        });
    }

    function overlay_remove() {
        kico.overlay.classList.add("remove");

        setTimeout(function () {
            if(document.querySelector("body > .bk-overlay")){
                kico.overlay.classList.remove("remove");
                document.body.removeChild(kico.overlay);
            }
        }, 300);
    }
}

// 图片放大
kico.image_box = document.createElement("div");
kico.image_box.className = "bk-image";
kico.image_single = document.createElement("img");
kico.image_box.appendChild(kico.image_single);

function bk_image(selector) {
    var get_images = document.querySelectorAll(selector);

    function item(obj) {
        obj.setAttribute("bk-image", "active");
        obj.addEventListener("click", function () {
            kico.image_single.src = obj.src;
            if(!document.querySelector("body > .bk-image")){
                document.body.appendChild(kico.image_box);
            }
        });
    }

    for(var i = 0; i < get_images.length; i++){
        item(get_images[i]);
    }

    kico.image_box.addEventListener("click", function () {
        kico.image_box.classList.add("remove");
        setTimeout(function () {
            try{
                document.body.removeChild(kico.image_box);
                kico.image_box.classList.remove("remove");
            }
            catch (err){}
        }, 300);
    });
}

$("#img").click(function change() {

});
$(document).ready(function () {
    $.ajax({
        type: "get",
        url: "/getUserInfo",
        dataType: "json",
        success: function (data) {
            $(".avatar").append("<img id='img' src='"+data['avatarUrl'] +  "' title='我是你的头像' />");
            $("#part1").append("<p>用户名："+data['username'] +"</p>");
            $("#part2").append("<p>昵称："+data['nickname'] +"</p>");

            $("#navigation").append("<a href='#' data-toggle='modal' data-target='#change'>修改信息</a>");
            $("#changeNickname").click(function () {
                var nickname = $('#nickname');
                var nicknameValue = nickname.val();
                if (nicknameValue == null || nicknameValue.length == 0){
                    alert("小老弟想当无名氏？你问问我40米的大刀同不同意")
                }else{
                    $.ajax({
                        type:"post",
                        url:"/updateNickname",
                        dataType:"json",
                        data:{
                            nickname: nicknameValue
                        },
                        success:function (data) {
                            if (data['status'] == 200){
                                alert("ok,已更新");
                                window.location.reload()
                            }else if (data['status'] == 500){
                                alert("这个名号已经被使用了，为了让仇家能准确地找到你换一个吧 -.-")
                            }
                        },
                        error:function () {
                            alert("请求错误")
                        }
                    })
                }
            });
            

        }
    })
});

function imgChange(e) {
    var dom = document.getElementById("avatarChange").files[0];
    if(!/image\/\w+/.test(dom.type)){
        alert("请选择图片")
        return false;
    }

    var reader = new FileReader();
    reader.onload = function (file) {
        return function (e) {
                var info = this.result;
                console.log(info);
            $.ajax({
                type: 'post',
                url: '/uploadAvatar',
                dataType: 'json',
                data: {
                    imgInfo: info
                },
                success:function (data) {
                    if (data['status'] != 200){
                        alert("更新头像出了点错误，再试一次吧")
                    }else {
                        alert("更新成功");
                        window.location.reload();
                    }

                },
                error:function () {
                    console.log("上传失败")
                }

            })
        }
    }(e.target.files[0]);
    reader.readAsDataURL(e.target.files[0]);

}
// 请保留版权说明
// if (window.console && window.console.log) {
//     console.log("\n %c Kico Style %c https://www.binkic.com \n\n","color: #fff; background: #3498db; padding: 5px 0;","background: #efefef; padding: 5px 0; text-decoration: none;");
// }